/*
 *--------------------------------------------------------------------------
 *   Battlefield - A Realtime Network Multiplayer Game
 *   =======================================================
 *   Developed by Group D02 - 2009/2010 Semester 4 - CS2103
 *     Harry Nguyen Duy Hoang <nnduyhoang@yahoo.co.uk>
 *     Kent Chng Siang Rong <fivefootway@gmail.com>
 *     Lim Yong Peng <limpeng1986@gmail.com>
 *     Loh Xiankun <u0807185@nus.edu.sg>
 *   Instructed by
 *     Dr. Damith C.Rajapakse <damith@gmail.com>
 *   =======================================================
 *   $Id: Screen.java 658 2010-07-30 08:32:19Z Harry $
 *   $LastChangedDate: 2010-07-30 08:32:19 +0000 (Fri, 30 Jul 2010) $
 *   $LastChangedBy: Harry $
 *--------------------------------------------------------------------------
 */
package battlefield.ui.screen;

import battlefield.Battlefield;
import battlefield.BattlefieldServerInterface;
import battlefield.entity.Game;
import battlefield.entity.Player;
import battlefield.entity.Session;
import battlefield.net.Network;
import battlefield.ui.GameUI;
import java.awt.Image;
import java.rmi.RemoteException;
import javax.swing.JPanel;

/**
 *
 * @author Harry Nguyen
 */
public class Screen extends JPanel {

    private Image backgroundImage;

    public Screen() {
        setOpaque(false);
    }

    public Image getBackgroundImage() {
        return backgroundImage;
    }

    public void setBackgroundImage(Image backgroundImage) {
        this.backgroundImage = backgroundImage;
    }

    /* Begin Region: State Pattern */
    public Screen home() {
        return this;
    }

    public Screen back() {
        return this;
    }

    public Screen showOptions() {
        return this;
    }

    public Screen showTutorials() {
        return this;
    }

    public Screen showCredits() {
        return this;
    }

    public Screen save() {
        return this;
    }

    public Screen createNewGame() {
        return this;
    }

    public Screen joinNetworkGame() {
        return this;
    }

    public Screen chooseMap() {
        return this;
    }

    public Screen waitForPlayers() {
        return this;
    }

    public Screen startTactics() {
        return this;
    }

    public Screen startWar() {
        return this;
    }

    public Screen win() {
        return this;
    }

    public Screen lose() {
        return this;
    }

    /* End Region: State Pattern */

    public GameUI getGUI() {
        return Battlefield.getInstance().getGUI();
    }

    public BattlefieldServerInterface getLogic() {
        return Battlefield.getInstance().getLogic();
    }

    public Network getNetwork() {
        return Battlefield.getInstance().getNetwork();
    }

    public Session getSession() {
        return Battlefield.getInstance().getSession();
    }

    public Player getPlayer(boolean refresh) {
        if (refresh && getSession().getPlayer() != null) {
            try {
                getSession().setPlayer(getLogic().getPlayer(getSession().getPlayer().getId()));
            } catch (RemoteException ex) {
                ex.printStackTrace();
            }
        }

        return getSession().getPlayer();
    }

    public Player getPlayer() {
        return getPlayer(false);
    }

    public Player getOpponent(boolean refresh) {
        if (getSession().getOpponent() == null) {
            try {
                getSession().setOpponent(getLogic().getOpponent(getPlayer().getId()));
            } catch (RemoteException ex) {
                ex.printStackTrace();
            }
        } else if (refresh) {
            try {
                getSession().setOpponent(getLogic().getPlayer(getSession().getOpponent().getId()));
            } catch (RemoteException ex) {
                ex.printStackTrace();
            }
        }

        return getSession().getOpponent();
    }

    public Player getOpponent() {
        return getOpponent(false);
    }

    public Game getGame(boolean refresh) {
        if (getSession().getGame() == null) {
            try {
                getSession().setGame(getLogic().getCurrentGame(getPlayer().getId()));
            } catch (RemoteException ex) {
                ex.printStackTrace();
            }
        } else if (refresh) {
            try {
                getSession().setGame(getLogic().getGame(getSession().getGame().getId()));
            } catch (RemoteException ex) {
                ex.printStackTrace();
            }
        }

        return getSession().getGame();
    }

    public Game getGame() {
        return getGame(false);
    }

    @Override
    public void paintComponent(java.awt.Graphics g) {
        if (backgroundImage != null) {
            g.drawImage(backgroundImage, 0, 0, this);
        }
        super.paintComponent(g);
    }
}
